<?PHP
class SkinController{

  private static $skinNames = array();

  public static function getCurrentSkinId(){
    return Settings::getInstance()->get("selectedskin");
  }
  public static function getCurrentMobileSkinId(){
    return Settings::getInstance()->get("selectedmobileskin");
  }

  public static function getCurrentSkinName(){
    if(Mobile::isMobileDevice()){
      return self::getCurrentMobileSkinName();
    }
    else{
      return self::getCurrentDesktopSkinName();
    }
  } 
  
  public static function getCurrentMobileSkinName(){
    $res = $GLOBALS['db']->ReadField("SELECT name FROM {'dbprefix'}skins WHERE id = '".SkinController::getCurrentMobileSkinId()."'");
    if($res){
      return $res;
    }
    else{
      return "mobile";
    }  
  }

  public static function getSkinName($id){
    $res = "";
    if(isset(self::$skinNames[$id])){
      $res = self::$skinNames[$id];
    }
    else{
      $id = $GLOBALS['db']->EscapeString($id);
      $res = $GLOBALS['db']->ReadField("SELECT name FROM {'dbprefix'}skins WHERE id = '".$id."'");
      self::$skinNames[$id] = $res;
    }
    return $res;
  }
  
  public static function getCurrentDesktopSkinName(){
    if($_GET['blank'] == "true"){
      $res =  "blank";
    }
    else{
      if((isset($_GET['skin']) || isset($_GET['save_settings'])) && $_SESSION['user']->isAdmin()){
        if(isset($_GET['save_settings']) && $_SESSION['user']->isAdmin()){
          $res = "backenddefault";
        }
        else{
          $res = $_GET['skin'];
        }
      }
      else {
        $res = self::getSkinName(SkinController::getCurrentSkinId());
      }
    }
    if($res){
      return $res;
    }
    else{
      return "default";
    }  
  }

  public static function getCurrentSkinPath(){
    return "system/skins/".SkinController::getCurrentSkinName();
  }

  public static function getInstalledSkins(){
    return $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}skins");
  }

  public static function displayCurrent(){
    $skin = self::getCurrentSkinName();
    include(Settings::getInstance()->get("root")."/system/skins/".$skin."/index.php");
  }
}
?>